package com.jic.order.controller;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.RestResult;
import com.jic.member.constant.consist.ResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.order.model.vo.request.MAbnormalOrderRequest;
import com.jic.order.model.vo.response.MAbnormalOrderResponse;
import com.jic.order.model.entity.MAbnormalOrder;
import com.jic.order.service.MAbnormalOrderService;

/**
 * @Author: 上海奇陌科技有限公司
 * @date: 2020-11-28 16:06:13
 */
@RestController
@Slf4j
@RequestMapping("/mAbnormalOrder")
@Api(tags = " [异常单列表]控制类-PC")
public class MAbnormalOrderController {
    @Autowired
    private MAbnormalOrderService service;


    /**
     * 新增记录
     *
     * @param request 新增[异常单列表]请求参数
     * @return MAbnormalOrderResponse 新增[异常单列表]响应参数
     * author 上海奇陌科技有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[异常单列表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody MAbnormalOrderRequest request) throws Throwable {
        MAbnormalOrder mAbnormalOrder = new MAbnormalOrder();
        BeanUtils.copyProperties(request, mAbnormalOrder);
        long flag = service.save(mAbnormalOrder);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param map 新增或修改[异常单列表]请求参数
     * @return MAbnormalOrderResponse 新增或修改[异常单列表]响应参数
     * author 上海奇陌科技有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[异常单列表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody MAbnormalOrderRequest request) throws Throwable {
        MAbnormalOrder mAbnormalOrder = new MAbnormalOrder();
        Long   flag=0L;
        for (MAbnormalOrder mp : request.getSelectionChangeList()) {
            request.setAbnormalCode(mp.getOrderNo());
            BeanUtils.copyProperties(request, mAbnormalOrder);
            flag+= service.saveOrUpdate(mAbnormalOrder);
        }
     
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[异常单列表]请求参数
     * @return MAbnormalOrderResponse 删除[异常单列表]响应参数
     * author 上海奇陌科技有限公司`
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[异常单列表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody MAbnormalOrderRequest request) throws Throwable {
        MAbnormalOrder mAbnormalOrder = new MAbnormalOrder();
        BeanUtils.copyProperties(request, mAbnormalOrder);
        long flag = service.deleteByPrimaryKeyLogically(mAbnormalOrder);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[异常单列表]请求参数
     * @return MAbnormalOrderResponse 修改[异常单列表]响应参数
     * author 上海奇陌科技有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[异常单列表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody MAbnormalOrderRequest request) throws Throwable {
        MAbnormalOrder mAbnormalOrder = new MAbnormalOrder();
        BeanUtils.copyProperties(request, mAbnormalOrder);
        long flag = service.updateByPrimaryKeySelective(mAbnormalOrder);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[异常单列表]详情请求参数
     * @return MAbnormalOrderResponse 查询[异常单列表]详情响应参数
     * author 上海奇陌科技有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[异常单列表]详情")
    @ResponseBody
    public RestResult<MAbnormalOrderResponse> getByPrimaryKey(@Validated @RequestBody MAbnormalOrderRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            MAbnormalOrder mAbnormalOrder = new MAbnormalOrder();
            BeanUtils.copyProperties(request, mAbnormalOrder);
            MAbnormalOrder result = service.getByPrimaryKey(mAbnormalOrder);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            MAbnormalOrderResponse bo = new MAbnormalOrderResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new MAbnormalOrderResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<MAbnormalOrder>> listPage(@RequestBody MAbnormalOrderRequest request) throws Throwable {
        MAbnormalOrder param = new MAbnormalOrder();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }


    /**
     * M_ABNORMAL_ORDER 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [异常单列表] ")
    @ResponseBody
    public RestResult<List<MAbnormalOrderResponse>> listMAbnormalOrderAllRecord(@RequestBody MAbnormalOrderRequest request) throws Throwable {
        MAbnormalOrder param = new MAbnormalOrder();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<MAbnormalOrder> list = service.listAllRecord(param);
        List<MAbnormalOrderResponse> responseList = list.stream().map(temp -> {
            MAbnormalOrderResponse bo = new MAbnormalOrderResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

}

	
